package com.notecalendar.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.notecalendar.dao.Evernote_FileDao;
import com.notecalendar.model.Evernote_File;

/**
 * Class for implementing the methods definite in Evernote_FileDao
 * @author angelcereijo
 *
 */
public class Evernote_FileDaoImpl extends HibernateDaoSupport implements Evernote_FileDao{
	
	/**
	 * @see Evernote_FileDao#insertEvernote_File(Evernote_File)
	 */
	public Evernote_File insertEvernote_File(Evernote_File evernote_file) {
		
		evernote_file.setId(getEvernote_fileMaxId());
		getHibernateTemplate().save(evernote_file);
		
		return evernote_file;
	}
	
	/**
	 * @see Evernote_FileDao#findEvernote_File(int)
	 */
	@SuppressWarnings("unchecked")
	public Evernote_File findEvernote_File(BigInteger id) {
		Criteria criteria = getSession().createCriteria(Evernote_File.class);
		List<Evernote_File> lista = criteria.add(Restrictions.eq("id", id)).list();
		
		return lista.size()>0?lista.get(0):null;
	}

	/**
	 * @see Evernote_FileDao#findAllEernote_File()
	 */
	@SuppressWarnings( "unchecked")
	public List<Evernote_File> findAllEvernote_File() {
		return getHibernateTemplate().find("from Evernote_File");
	}
	
	/**
	 * @see Evernote_FileDao#findAllEvernote_FileUser(String)
	 */
	@SuppressWarnings("unchecked")
	public List<Evernote_File> findAllEvernote_FileUser(BigInteger evernote_id){
		Criteria criteria = getSession().createCriteria(Evernote_File.class);
		List<Evernote_File> lista = criteria.add(Restrictions.eq("evernote_id", evernote_id)).list();
		
		return lista;
	}
	
	
	/**
	 * @see Evernote_FileDao#deleteEvernote_File(Evernote_File)
	 */
	public void deleteEvernote_File(Evernote_File evernote_file) {
		getHibernateTemplate().delete(evernote_file);

	}
	
	
	/**
	 * Get the max id  from evernote_file table 
	 * @return
	 */
	private BigInteger getEvernote_fileMaxId() {
		Query query = getSession().getNamedQuery("evernote_fileMaxId");
		BigInteger id = (BigInteger)query.list().get(0);
		id = (id==null)?new BigInteger("0"):id;
		
		return id;
	}

}
